package com.erp.order.controller;

import cn.hutool.core.bean.BeanUtil;
import com.erp.order.domain.bo.ErpSellerChildOrderExtendBo;
import com.erp.order.domain.vo.ErpSellerChildOrderExtendVo;
import com.erp.order.service.IErpSellerChildOrderExtendService;
import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.bean.BeanCopyUtils;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.order.api.domain.ErpSellerChildOrderExtend;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 卖家子订单延伸
 *
 * @author erp
 * @date 2023-06-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/purchase/sellerChildOrderExtend")
public class ErpSellerChildOrderExtendController extends BaseController {

    private final IErpSellerChildOrderExtendService iErpSellerChildOrderExtendService;

    /**
     * 根据子订单号获取子订单拓展信息
     */
    @GetMapping("/getChildExtendByChildOrder/{childOrderId}")
    ErpSellerChildOrderExtend getChildExtendByChildOrder(@PathVariable("childOrderId") String childOrderId) {
        return iErpSellerChildOrderExtendService.getChildExtendByChildOrder(childOrderId);
    }

    /**
     * 根据订单号获取子订单拓展信息
     */
    @GetMapping("/getChildExtendByOrder/{orderId}")
    List<ErpSellerChildOrderExtend> getChildExtendByOrder(@PathVariable("orderId") Long orderId) {
        return iErpSellerChildOrderExtendService.getChildExtendByOrderId(orderId);
    }

    /**
     * 确认已打印面单
     */
    @GetMapping("/printed")
    public R<Object> printed(String childOrderId) {
        return iErpSellerChildOrderExtendService.printed(childOrderId);
    }

    /**
     * 查询卖家子订单延伸列表
     */
    @GetMapping("/list")
    public TableDataInfo<ErpSellerChildOrderExtendVo> list(ErpSellerChildOrderExtendBo bo, PageQuery pageQuery) {
        return iErpSellerChildOrderExtendService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出卖家子订单延伸列表
     */
//    @Log(title = "卖家子订单延伸", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(ErpSellerChildOrderExtendBo bo, HttpServletResponse response) {
//        List<ErpSellerChildOrderExtendVo> list = iErpSellerChildOrderExtendService.queryList(bo);
//        ExcelUtil.exportExcel(list, "卖家子订单延伸", ErpSellerChildOrderExtendVo.class, response);
//    }

    /**
     * 获取卖家子订单延伸详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<ErpSellerChildOrderExtendVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iErpSellerChildOrderExtendService.queryById(id));
    }

    /**
     * 新增卖家子订单延伸
     */
    @Log(title = "卖家子订单延伸", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpSellerChildOrderExtendBo bo) {
        return toAjax(iErpSellerChildOrderExtendService.insertByBo(bo));
    }

    /**
     * 修改卖家子订单延伸
     */
    @Log(title = "卖家子订单延伸", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpSellerChildOrderExtendBo bo) {
        return toAjax(iErpSellerChildOrderExtendService.updateByBo(bo));
    }

    /**
     * 修改子订单拓展表信息
     */
    @PutMapping("/updateChildExtend")
    public R<Void> updateChildExtend(@RequestBody ErpSellerChildOrderExtend childOrderExtend) {
        ErpSellerChildOrderExtendBo bo = BeanUtil.toBean(childOrderExtend, ErpSellerChildOrderExtendBo.class);
        if (childOrderExtend.getId() != null) {
            return toAjax(iErpSellerChildOrderExtendService.updateByBo(bo));
        }else {
            return toAjax(iErpSellerChildOrderExtendService.insertByBo(bo));
        }
        
    }

    /**
     * 删除卖家子订单延伸
     *
     * @param ids 主键串
     */
    @Log(title = "卖家子订单延伸", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iErpSellerChildOrderExtendService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
